<?php

class ArticleController extends Zend_Controller_Action 
{
    function init()
    {
        Zend_Loader::loadClass('Mainmenu');
        Zend_Loader::loadClass('Article');
        Zend_Loader::loadClass('Translation');
        Zend_Loader::loadClass('User');
        $this->initView();
        $this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->user = Zend_Auth::getInstance()->getIdentity();
        $article = new Article();
        $this->view->categories = $article->getCategories();
    }
    
    function preDispatch()
    {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('auth/login');
        }
        $menu = new Mainmenu();
        $this->view->main_menu = $menu->getMenu($this->view->user->id);
    }
    
    function indexAction()
    {
        $this->view->title = "Articles";
        $this->view->content_title = "Articles";
        
        $Lang = $this->_request->getParam('lang');
        if($Lang) $this->view->language = $Lang;
        else $this->view->language = DEFAUL_LANGUAGE;
        
        $article = new Article();
        $this->view->items = $article->fetchAll('deleted!=1',array('category_id', 'updated_at DESC'));
        if($this->view->language != DEFAUL_LANGUAGE) {
            $translation = new Translation();
            foreach($this->view->items as $item) {
                $t = $translation->getArticle($item->id,$this->view->language);
                if($t) foreach($t as $field=>$value){
                    $item->$field = $value;
                }
                
            }
        }
        
        
        $this->view->can_add = true;
        $this->view->can_edit = true;
        $this->view->can_delete = true;
        
    }
    
    
    function validate($id,&$params)
    {
        $valid = true;
        $obj = new Article();
        
        if(strlen($params['alias'])==0)
        {
            $params['alias'] = 'article'.($obj->getMaxId()+1).'.html';
        }    
        if(strlen($params['subject'])==0)
        {
            $this->view->message .= 'Please, enter Subject.<br>';
            $valid = false;
        }    
        if(strlen($params['content'])==0)
        {
            $this->view->message .= 'Please, enter Content.<br>';
            $valid = false;
        }    
        if(strlen($params['real_date'])==0)
        {
            $this->view->message .= 'Please, enter Real_date.<br>';
            $valid = false;
        }    
        if($valid)
        {
            if($obj->fetchRow('id <> '.$id.' and alias="'.$params['alias'].'"'))
            {
                $this->view->message .= 'Article : Alias '.$params['alias'].' already exist.<br>';
                $valid = false;
            }
        }        
        
        //email
        $email_pattern = '/^[A-Z0-9\._\-]+@[A-Z0-9\.\-]+\.[A-Z]{2,6}$/i';
        
        //date 
        $date_pattern = '/^[0-9]{4}\-(?:0[1-9]|1[0-2])\-(?:[0-2]?[0-9]|3[0-1])$/';
        if(!preg_match($date_pattern,$params['real_date']))
        {
            $this->view->message .= 'Invalid real_date '.$params['real_date'].'. Format yyyy-mm-dd.<br>';
            $valid = false;
        } 
        return $valid;
    }
    
    function addAction()
    {
        $params = array();
        
        $this->view->content_title = $this->view->title = "Add New Article";
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            Zend_Loader::loadClass('Zend_Filter_StripTags');
            $filter = new Zend_Filter_StripTags();
            
            $params['alias'] = trim($filter->filter($this->_request->getPost('alias')));
            
            $params['content'] = preg_replace('/[\\]+\"/','"',trim($this->_request->getPost('content')));
            $params['short_content'] = preg_replace('/[\\]+\"/','"',trim($this->_request->getPost('short_content')));
            
            $params['subject'] = trim($filter->filter($this->_request->getPost('subject')));
            $params['category_id'] = (int)trim($filter->filter($this->_request->getPost('category_id')));
            $params['published'] = trim($filter->filter($this->_request->getPost('published')));
            $params['created_at'] = date('Y-m-d H:i:s');
            $params['created_by'] = $this->view->user->id;
            $params['updated_at'] = date('Y-m-d H:i:s');
            $params['updated_by'] = $this->view->user->id;
            $params['picture'] = trim($filter->filter($this->_request->getPost('picture')));
            $params['path'] = trim($filter->filter($this->_request->getPost('path')));
            $params['real_date'] = trim($filter->filter($this->_request->getPost('real_date')));
            $params['text_date'] = trim($filter->filter($this->_request->getPost('text_date')));
            $params['for_print'] = trim($filter->filter($this->_request->getPost('for_print')));
                    
            $this->view->message = '';
            if($this->validate(0,$params)) {
                $obj = new Article();
                $new_id = $obj->insert($params);
                
                $this->_redirect('/article');
                return;
            }
        } 
        
        // set up an "empty" article        
        $this->view->item = new stdClass();
        $this->view->item->id = 0;
        $this->view->item->alias = isset($params['alias'])?$params['alias']:'';
        $this->view->item->content = isset($params['content'])?$params['content']:'';
        $this->view->item->short_content = isset($params['short_content'])?$params['short_content']:'';
        $this->view->item->subject = isset($params['subject'])?$params['subject']:'';
        $this->view->item->category_id = isset($params['category_id'])?$params['category_id']:'';
        $this->view->item->published = isset($params['published'])?$params['published']:1;
        $this->view->item->created_at = isset($params['created_at'])?$params['created_at']:date('Y-m-d H:i:s');
        $this->view->item->created_by = isset($params['created_by'])?$params['created_by']:'';
        $this->view->item->updated_at = isset($params['updated_at'])?$params['updated_at']:date('Y-m-d H:i:s');
        $this->view->item->updated_by = isset($params['updated_by'])?$params['updated_by']:'';
        $this->view->item->picture = isset($params['picture'])?$params['picture']:'';
        $this->view->item->path = isset($params['path'])?$params['path']:'';
        $this->view->item->real_date = isset($params['real_date'])?$params['real_date']:date('Y-m-d');
        $this->view->item->text_date = isset($params['text_date'])?$params['text_date']:'';
        $this->view->item->for_print = isset($params['for_print'])?$params['for_print']:'';
                
        // additional view fields required by form
        
        $this->view->publisheds = array(1=>'Yes',0=>'No');
        $this->view->for_prints = array(1=>'Yes',0=>'No');
        $this->view->action = 'add';
        
        $this->render();
    }
    
    function editAction()
    {
        $this->view->content_title = $this->view->title = "Edit Article";
        $obj = new Article();
        
        $params = array();
        
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            Zend_Loader::loadClass('Zend_Filter_StripTags');
            $filter = new Zend_Filter_StripTags();
            
            $id = (int)$this->_request->getPost('id');
            
            $params['alias'] = trim($filter->filter($this->_request->getPost('alias')));
            $params['content'] = preg_replace('/[\\]+\"/','"',trim($this->_request->getPost('content')));
            $params['short_content'] = preg_replace('/[\\]+\"/','"',trim($this->_request->getPost('short_content')));
            
            $params['subject'] = trim($filter->filter($this->_request->getPost('subject')));
            $params['category_id'] = (int)trim($filter->filter($this->_request->getPost('category_id')));
            $params['published'] = trim($filter->filter($this->_request->getPost('published')));
            $params['updated_at'] = date('Y-m-d H:i:s');
            $params['updated_by'] = $this->view->user->id;
            $params['picture'] = trim($filter->filter($this->_request->getPost('picture')));
            $params['path'] = trim($filter->filter($this->_request->getPost('path')));
            $params['real_date'] = trim($filter->filter($this->_request->getPost('real_date')));
            $params['text_date'] = trim($filter->filter($this->_request->getPost('text_date')));
            $params['for_print'] = trim($filter->filter($this->_request->getPost('for_print')));
                        
            if ($id !== false) {
                $this->view->message = '';
                if($this->validate($id,$params)) {
                    $where = 'id = ' . $id;
                    $obj->update($params, $where);
                    
                    $this->_redirect('/article');
                    return;
                } else {
                    $this->view->item = $obj->fetchRow('id='.$id);
                    $this->view->item->id = $id;
                    if( $params['alias']) $this->view->item->alias = $params['alias'];
                    if( $params['content']) $this->view->item->content = $params['content'];
                    if( $params['short_content']) $this->view->item->short_content = $params['short_content'];
                    if( $params['subject']) $this->view->item->subject = $params['subject'];
                    if( $params['category_id']) $this->view->item->category_id = $params['category_id'];
                    if( $params['published']) $this->view->item->published = $params['published'];
                    if( $params['created_at']) $this->view->item->created_at = $params['created_at'];
                    if( $params['created_by']) $this->view->item->created_by = $params['created_by'];
                    if( $params['updated_at']) $this->view->item->updated_at = $params['updated_at'];
                    if( $params['updated_by']) $this->view->item->updated_by = $params['updated_by'];
                    if( $params['picture']) $this->view->item->picture = $params['picture'];
                    if( $params['path']) $this->view->item->path = $params['path'];
                    if( $params['real_date']) $this->view->item->real_date = $params['real_date'];
                    if( $params['text_date']) $this->view->item->text_date = $params['text_date'];
                    if( $params['for_print']) $this->view->item->for_print = $params['for_print'];
                                    
                }
            }
        } else {
            $id = (int)$this->_request->getParam('id', 0);
            if ($id > 0) {
                $this->view->item = $obj->fetchRow('id='.$id);
            }
        }

        // additional view fields required by form
        $this->view->action = 'edit';
        $this->view->publisheds = array('1'=>'Yes','0'=>'No');
        $this->view->for_prints = array('1'=>'Yes','0'=>'No');
        
        
        $this->render();
    }
    
    function deleteAction()
    {
        $this->view->content_title = $this->view->title = "Delete Article";
        
        $obj = new Article();
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            Zend_Loader::loadClass('Zend_Filter_Alpha');
            $filter = new Zend_Filter_Alpha();
            
            $id = (int)$this->_request->getPost('id');
            $del = $filter->filter($this->_request->getPost('del'));
            
            if ($del == 'Yes' && $id > 0) {
                $data = array(
                        'deleted' => '1'
                );
                $where = 'id = ' . $id;
                $obj->update($data, $where);
            }
        } else {
            $id = (int)$this->_request->getParam('id');
            if ($id > 0) {
                // only render if we have an id and can find the article.
                $this->view->item = $obj->fetchRow('id='.$id);
                
                if ($this->view->item->id > 0) {
                    $this->render();
                    return;
                }
            }
        }
        
        // redirect back to the project list unless we have rendered the view
        $this->_redirect('/article');
    }
    
}